﻿using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using Odyssey.Controls;
using CCF20Model;
namespace CCF20
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //private BackgroundWorker backgroundWorker;
        public static bool LoginSuccess = false;
        
        public MainWindow()
        {
            InitializeComponent();
            
            //backgroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker"));
            //backgroundWorker.RunWorkerAsync();
            
        }
        
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            if (((Button)sender).Name == "Reset")
            {
                ResetSystem();
                MessageBox.Show("系统重置成功","CCF20", MessageBoxButton.OK);
            }
            else
            {
                SelectorControl.EvalValue = ((Button)sender).Name;

                testCanExecute = !testCanExecute;
                CommandManager.InvalidateRequerySuggested();
            }
            
        }
        private void ResetSystem()
        {
            ModelFactory modelFactory = new ModelFactory();
            VehicleModel vm = (VehicleModel)modelFactory.CreateModel(ModelName.VehicleModel);
            vm.ExecuteSql(null, "delete clearcodehistory");
            vm.ExecuteSql(null, "delete faults");
            vm.ExecuteSql(null, "delete troubleddevice");
            vm.ExecuteSql(null, "delete devices");
            //can't delete customer,vehicle and transferedcustomer as they are in a group
            //vm.ExecuteSql(null, "delete transferedcustomers");
            vm.ExecuteSql(null, "delete contacthistory");
            vm.ExecuteSql(null, "delete smshistory");
            vm.ExecuteSql(null, "delete vehiclemaintenancehistory");
            vm.ExecuteSql(null, "delete vehiclemaintenancerecord");
            vm.ExecuteSql(null, "delete vehiclerepairhistory");
            vm.ExecuteSql(null, "update Vehicles set CurrentMileage=null,LastContactResult=null, LastServcieDate=null,LastServiceMileage = null,LatestActiveTime =null ");
        }
        private void LauchInStock()
        {
            ButtonAutomationPeer peer = new ButtonAutomationPeer(InStock);
            IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
            invokeProv.Invoke(); 
        }
            
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            this.tblCurrentUser.Text = (string)Application.Current.Properties["CurrentUser"];
            LoggerHelper.LogCCF20(string.Format("User:{0} Permission:{1}",this.tblCurrentUser.Text,(string)Application.Current.Properties["AllowedPermission"]));
            if (this.InStock.IsEnabled)
            {
                LauchInStock();
            }
            if (App.IsMobileBranch)
            {
                this.bar.Sections[1].IsEnabled = false;
                this.bar.Sections[2].IsEnabled = false;
                this.bar.Sections[3].IsEnabled = false;
                this.Title = this.Title + "营业厅版";
            }
        }

    

        private void OutlookSection_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            
        }

        //private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        //{


        //    BackGroundWorker.ConnectToDB();
        //    if (backgroundWorker.CancellationPending)
        //    {
        //        e.Cancel = true;
        //        return;
        //    }

        //    // Return the result.
        //    //e.Result = primes;
        //}
        //private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    if (e.Cancelled)
        //    {
        //        MessageBox.Show("Search cancelled.");
        //    }
        //    else if (e.Error != null)
        //    {
        //        // An error was thrown by the DoWork event handler.
        //        this.DBConnectStatus.Text = string.Format("数据库连接失败--{0}", e.Error.Message);
        //    }
        //    else
        //    {
        //        this.DBConnectStatus.Text = "数据库连接成功";
        //    }
            
        //}

        //private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        //{
            
        //}

    }
}
